//
//  DCMessageSender.m
//  xmppBase
//
//  Created by striby on 16/1/13.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCMessageSender.h"
#import "MBProgressHUD.h"
@implementation DCMessageSender

+(XMPPMessage *)messageWithMessageType:(NSString *)messageType andSubType:(NSString *)subType forString:(NSString *)string addtionXMLs:(NSArray *)addtionXMLs toUser:(XMPPJID *)targetJID autoSendToServer:(BOOL)autoSendToServer
{
    if (string.length < 1) {
        string = @"";
    }
    
    
    XMPPMessage *message = [[XMPPMessage alloc]initWithType:messageType to:targetJID];
    
    
    
    //消息时间
    [message addBody:string];
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    //发送时间
    DDXMLElement * time = [[DDXMLElement alloc]initWithName:messageSendTime stringValue:[formatter stringFromDate:[NSDate date]]];
    
    [message addChild:time];
    
    //消息类型
    [message addChild:[[DDXMLElement alloc]initWithName:subMessageType stringValue:subType]];
    
    //消息来源
    [message addChild:[[DDXMLElement alloc]initWithName:messageFrom stringValue:[DCXMPPTool shareInstance].xmppStream.myJID.full]];
    
    //消息编号
    [message addChild:[[DDXMLElement alloc]initWithName:messageID stringValue:[DCXMPPTool shareInstance].xmppStream.generateUUID]];
    
    
    if (addtionXMLs) {
        for (DDXMLElement * ele  in addtionXMLs) {
            [message addChild:ele];
        }
    }
    
    
    if (autoSendToServer) {
        
        __autoreleasing XMPPElementReceipt * receipt ;
         [[DCXMPPTool shareInstance].xmppStream  sendElement:message andGetReceipt:&receipt] ;
//        NSLog(@"%@",receipt);awea-
    }
    
#ifdef DEBUG
    XMPPUserMemoryStorageObject * object = [[DCXMPPTool shareInstance].xmppRosterMemoryStorage userForJID:targetJID];
    
    
    if (!object.isOnline&&[messageType isEqualToString:privateMessage]&&object!=nil) {
        UIView * view =[[UIApplication sharedApplication].delegate window].rootViewController.view;
        
        [MBProgressHUD hideAllHUDsForView:view animated:NO];
        
        MBProgressHUD * HUD =[MBProgressHUD HUDForView:view];
        HUD.mode = MBProgressHUDModeText;
        [view addSubview:HUD];
        HUD.labelText =@"对方不在线 离线消息";
        HUD.removeFromSuperViewOnHide=YES;
        [HUD show:YES];
        [HUD hide:YES afterDelay:1.2];
    }
    
   
    
#endif
    
    return message;
}



@end
